package cn.xsw.config;

import cn.xsw.interceptor.AuthInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer{

    @Bean
    public AuthInterceptor authInterceptor(){
        return  new AuthInterceptor();
    }

    @Bean
    public ObjectMapper objectMapper(){
        return  new ObjectMapper();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html","doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        //放行路径
        List<String> patterns = new ArrayList<String>();

        patterns.add("/comment/addComment");
        patterns.add("/appointment/addAppointment");
        patterns.add("/*/find*");
        patterns.add("/error");
        patterns.add("/user/login");
        patterns.add("/user/adminLogin");
        //放行swagger2
        patterns.add("/doc.html");
        patterns.add("//swagger-resources/**");
        patterns.add("/webjars/**");
        patterns.add("/v2/**");
         patterns.add("/swagger-ui.html/**");
        registry.addInterceptor(authInterceptor()).addPathPatterns("/**").excludePathPatterns(patterns);//除了patterns其他路径全部拦截

    }




}
